package com.xgy.userauthsystem.mapper;

import com.xgy.userauthsystem.entity.User;
import org.apache.ibatis.annotations.*;

/**
 * 用户数据访问接口
 * 使用MyBatis注解方式实现
 */
@Mapper
public interface UserMapper {

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户对象
     */
    @Select("SELECT id, username, password, email, created_at as createdAt, updated_at as updatedAt FROM users WHERE username = #{username}")
    User findByUsername(String username);

    /**
     * 根据邮箱查找用户
     * @param email 邮箱
     * @return 用户对象
     */
    @Select("SELECT id, username, password, email, created_at as createdAt, updated_at as updatedAt FROM users WHERE email = #{email}")
    User findByEmail(String email);

    /**
     * 检查用户名是否存在
     * @param username 用户名
     * @return 如果用户名存在返回true，否则返回false
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    boolean existsByUsername(String username);

    /**
     * 检查邮箱是否存在
     * @param email 邮箱
     * @return 如果邮箱存在返回true，否则返回false
     */
    @Select("SELECT COUNT(*) FROM users WHERE email = #{email}")
    boolean existsByEmail(String email);

    /**
     * 插入新用户
     * @param user 用户对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO users (username, password, email, created_at, updated_at) VALUES (#{username}, #{password}, #{email}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);
}